﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using LinkShareWpf.Commands;
using System.Windows.Forms;
using System.Collections.ObjectModel;
using UniCT.LinkShareWpf.Interface;
using System.Net;
using UniCT.LinkShareWpf.Model;

namespace LinkShareWpf.ViewModel
{
    public class ConnectionControlViewModel
    {

        #region Events
        public event EventHandler MessageBoxRequest;
        #endregion

        #region Commands
        public DelegateCommand StartStopCommand { get; private set; }
        public DelegateCommand AddNodeCommand { get; private set; }
        public DelegateCommand RemoveNodeCommand { get; private set; }
        public DelegateCommand PingsCommand { get; private set; }
        #endregion

        #region Private
        private ObservableCollection<Node> myNodes;
        #endregion

        #region Properties
        public ObservableCollection<Node> Nodes
        {
            get { return myNodes; }
            set { myNodes = value; }
        }
        #endregion

        public ConnectionControlViewModel()
        {
            PingsCommand = new DelegateCommand(o => PingsNodes());
            StartStopCommand = new DelegateCommand(o => StartStopServer());
            AddNodeCommand = new DelegateCommand(commandParameter => AddNode(commandParameter));
            RemoveNodeCommand = new DelegateCommand(commandParameter => RemoveNode(commandParameter));
            myNodes = ConnectionHandler.Instance.GetNodes();
            ConnectionHandler.Instance.UpdateXmlNodesRequest += UpdateXmlNodesEventHandler;
        }

        void UpdateXmlNodesEventHandler(object sender, EventArgs e)
        {
            myNodes = ConnectionHandler.Instance.GetNodes();
       }
        
        public void AddNode(object commandParameter)
        {
            string url = commandParameter as string;
            url = url.Replace(" ", "");
            string ip = url.Split(':')[0];
            string port = url.Split(':')[1];
            IPAddress temp;
            if (IPAddress.TryParse(ip, out temp))
            {
                Node node = new Node();
                node.LastAliveRequest = DateTime.Now;
                node.LastPing = -1;
                Uri uri = ConnectionHandler.Instance.GetUri(ip, Int32.Parse(port));
                node.Url = uri.ToString();
                // È giusto fare cosi e non aggiornare direttamente la lista dei nodi (qui dentro),
                // perché la modifica della lista puo avverenire anche 
                // per altre motivazioni fuori da questo contesto
                ConnectionHandler.Instance.AddNode(node, true);
            }
            else
                MessageBoxRequest(this, EventArgs.Empty);
        }
   
        public void PingsNodes()
        {
            ConnectionHandler.Instance.Pings();
        }

        private void RemoveNode(object commandParameter)
        {
            Node node = commandParameter as Node;
            ConnectionHandler.Instance.RemoveNode(node);
        }

        private void StartStopServer()
        {
            if (ConnectionHandler.Instance.ServerOnLine == true)
                ConnectionHandler.Instance.StopServer();
            else
                ConnectionHandler.Instance.StartServer();
        }

    }

}
